﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Mvc;
using sydfdl.TMS.BLL;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System.Linq;

namespace sydfdl.TMS.Areas.SystemData.Controllers
{
    public class GroupController : Controller
    {
        //
        // GET: /SystemData/Group/

        #region

        BLLT_B_Group bllGroup = new BLLT_B_Group();

        #endregion

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GroupMenu()
        {
            return View();
        }

        public ActionResult GroupUser(string gid)
        {
            ViewBag.GID = gid;
            return View();
        }

        public ActionResult GroupCompany(string gid)
        {
            ViewBag.GID = gid;
            return View();
        }

        #region 组
        public void GetGroup()
        {
            string xml = JsonHelp.GetParaXML(Request.Form.Get("pageIndex"), Request.Form.Get("pageSize"),
                Request.Form.Get("sortField"), Request.Form.Get("sortOrder"), Request.Form.Get("key"));

            Response.Write(JsonHelp.EntitiesToString(new BLLT_B_Group().GetList(xml)));
        }

        public void Save()
        {
            string json = @"[]";
            IEnumerable<T_B_Group> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Group>>(Request.Form.Get("add"));
            IEnumerable<T_B_Group> edit = Request.Form.Get("edit") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Group>>(Request.Form.Get("edit"));
            IEnumerable<T_B_Group> del = Request.Form.Get("del") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Group>>(Request.Form.Get("del"));

            Response.Write(new BLLT_B_Group().Save(add, edit, del));
        }
        #endregion

        #region 菜单
        public void GetGroupMenu()
        {
            string mid = Request.QueryString.Get("mid");

            Response.Write(JsonHelp.EntitiesToString(new BLLT_B_Group().GetGroupMenu(Guid.Parse(mid))));
        }

        public void SaveGroupMenu()
        {
            string jsonDecode = HttpUtility.UrlDecode(Request.Form.Get("edit"));
            Guid gid = Guid.Parse(Request.Form.Get("GID"));
            string json = @"[]";

            IEnumerable<T_B_GroupMenu> edit = jsonDecode == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_GroupMenu>>(jsonDecode);

            Response.Write(new BLLT_B_Group().SaveGroupMenu(gid, edit));
        }
        #endregion

        #region  组与用户

        public void GetUnSelectedUser()
        {
            string gid = Request["gid"];

            DataTable dt = bllGroup.GetUnSelectedUser(gid);

            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        public void GetSelectedUser()
        {
            string gid = Request["gid"];
            string xml = JsonHelp.GetParaXML(Request.Form.Get("pageIndex"), Request.Form.Get("pageSize"),
                Request.Form.Get("sortField"), Request.Form.Get("sortOrder"), Request.Form.Get("key"));

            var list = bllGroup.GetSelectedUser(gid, xml);

            var str = JsonHelp.EntitiesToString(list);

            Response.Write(str);
        }

        public void SaveGroupUser()
        {
            string json = @"[]";

            IEnumerable<T_B_User_Group> add = Request.Form.Get("add") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_User_Group>>(Request.Form.Get("add"));

            IEnumerable<T_B_User_Group> remove = Request.Form.Get("remove") == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_User_Group>>(Request.Form.Get("remove"));

            Response.Write(bllGroup.SaveGroupUser(add, remove));
        }

        #endregion

        #region 组与公司

        public void GetAllCompany(string gid)
        {
            DataTable dt = bllGroup.GetAllCompany(gid);

            string str = MiniUIJson.Encode(dt);

            Response.Write(str);
        }

        public void SaveGroupCompanyData()
        {
            string gid = Request.Form.Get("gid") ?? string.Empty;
            string json = @"[]";
            string editJson = HttpUtility.UrlDecode(Request.Form.Get("edit"));
            IEnumerable<T_B_Company_Group> edit = editJson == json ? null : JsonHelp.ConvertJsonToEntity<IEnumerable<T_B_Company_Group>>(editJson);
            Response.Write(bllGroup.SaveGroupCompanyData(edit, gid));
        }


        #endregion

    }
}
